package ContainerLayer;
import java.util.*;

/**
 * Write a description of class Order here.
 * This class Order is container for PartOrder class
 * 
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Order
{
    // instance variables - replace the example below with your own
    private ArrayList<PartOrder> partorders;
    private Costumer costumerName;
    private Salesman salesmanName;
    private long id;
    private String date;
    private String dateModify;
    private boolean done;

    /**
     * Constructor for objects of class Order
     */
    public Order(long id, String date, String dateModify, boolean done, Costumer costumerName, Salesman salesmanName)
    {
        // initialise instance variables
        this.id = id;
        this.date = date;
        this.dateModify = dateModify;
        this.done = done;
        this.costumerName = costumerName;
        this.salesmanName = salesmanName;        

        partorders = new ArrayList<PartOrder>();

    }

    public void setId(long id)
    { 
        this.id = id;
    }

    public void setDate(String date)
    {        
        this.date = date;
    }

    public void setDateModify(String dateModify)
    {
        this.dateModify = dateModify;
    }

    public void setDone(boolean done)
    {
        this.done = done;
    }

    public void setCostumerName(Costumer costumerName)
    {
        this.costumerName = costumerName;
    }

    public void setSalesmanName(Salesman salesmanName)
    {
        this.salesmanName = salesmanName;
    }

    public long getID()
    {
        return id;
    }

    public String getDate()
    {
        return date;
    }

    public String getDateModify()
    {
        return dateModify;
    }

    public boolean getDone()
    {
        return done;
    }

    public Costumer getCostumerName()
    {
        return costumerName;
    }

    public Salesman getSalesmanName()
    {
        return salesmanName;
    }

    //following methods are to partOrder class 
    public void addPartOrder(PartOrder partOrder)
    {
        if(!partorders.equals(partOrder))
        {
            partorders.add(partOrder);
        }
        else
        {
            System.out.println("There is already a order with the typed name");
        }
    }

    public PartOrder readPartOrder(long id)
    {
        int index = 0;
        boolean found = false;
        int partOrderIndex = 0;

        while(index < partorders.size() && !found)
        {
            if(partorders.get(index).getID() == id)
            {
                partOrderIndex = index;
                found = true;
            }
            index++;
        }
        if(found)
        {
            return partorders.get(partOrderIndex);
        }
        else
        {
            return null;
        }
    }

    public void updatePartOrder(long id, ProductDescription description, ProductNo item)
    {
        int index = 0;
        boolean found = false;
        int partOrderIndex = 0;

        while(index < partorders.size() && !found)
        {
            if(partorders.get(index).getID() == id)
            {
                partOrderIndex = index;
                found = true;
            }
            index++;
        }
        if(found)
        {
            PartOrder po = partorders.get(partOrderIndex);
            if(description != null)
            {
                po.updateProductDescription(description);
            }
            if(item != null)
            {
                po.updateProductNo(item);
            }            
        }
        else
        {
            System.out.println("There is no Partorder with the typed ID,  - try again with another ID");
        }
    }

    public void deletePartOrder(long id)
    {
        int index = 0;
        boolean found = false;
        int partOrderIndex = 0;

        while(index < partorders.size() && !found)
        {
            if(partorders.get(index).getID() == id)
            {
                partOrderIndex = index;
                found = true;
            }
            index++;
        }
        if(found)
        {
            partorders.remove(partOrderIndex);
        }
        else
        {
            System.out.println("There is no Partorder with the typed ID,  - try again with another ID");
        }
    }

    public void printOnePartOrder(long id)
    {
        int index = 0;
        boolean found = false;
        int partOrderIndex = 0;

        while(index < partorders.size() && !found)
        {
            if(partorders.get(index).getID() == id)
            {
                partOrderIndex = index;
                found = true;
            }
            index++;
        }
        if(found)
        {
            PartOrder po = partorders.get(partOrderIndex);

            System.out.println("############################################################");
            System.out.println("#     Order ID: "+po.getID());
            System.out.println("#  ---------------------------------------------------------");
            System.out.println("#        Product Description: "+po.readProductDescription());
            System.out.println("#  ProductNo: "+po.readProductNo());                            
            System.out.println("########   THE END  for order ID: "+po.getID());
            System.out.println("");  
        }
        else
        {
            System.out.println("There is no Partorder with the typed ID,  - try again with another ID");
        }
    }

    public void printAllPartorder()
    {
        if(partorders != null)
        {
            for(PartOrder po : partorders)
            {
                System.out.println("############################################################");
                System.out.println("#     Order ID: "+po.getID());
                System.out.println("#  ---------------------------------------------------------");
                System.out.println("#        Product Description: "+po.readProductDescription());
                System.out.println("#  ProductNo: "+po.readProductNo());                            
                System.out.println("########   THE END  for order ID: "+po.getID());
                System.out.println("");  
            }
        }

    }
    
    public int getSizeOfPartOrder()
    {
        if(partorders != null)
        {
            return partorders.size();
        }
        else
        {
            return 0;
        }
    }
    

}
